package com.marver;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.marver.common.filter.TokenAuthorFilter;
import com.marver.common.jackson.MyBeanSerializerModifier;
import com.marver.common.listener.IndexListener;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.TaskExecutor;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication()
@EnableAsync
@MapperScan(basePackages = "com.marver.*.mapper")
public class ApiApplication {
    
	public static void main(String[] args) {
		SpringApplication.run(ApiApplication.class, args);
	}
	
	/**
	 * 请求过滤器，判断token有效性
	 * @return
	 */
    @Bean
    public FilterRegistrationBean  basicFilterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();  
        TokenAuthorFilter httpBasicFilter = new TokenAuthorFilter();
        registrationBean.setFilter(httpBasicFilter);
        List<String> urlPatterns = new ArrayList<String>();
        urlPatterns.add("/*");  
        registrationBean.setUrlPatterns(urlPatterns);
        registrationBean.setOrder(1);
        return registrationBean;  
    }
    
    @Bean
    public ServletListenerRegistrationBean<IndexListener> servletListenerRegistrationBean(){
        ServletListenerRegistrationBean<IndexListener> servletListenerRegistrationBean = new ServletListenerRegistrationBean();
        servletListenerRegistrationBean.setListener(new IndexListener());
        return servletListenerRegistrationBean;
    }

    /**
     * spring boot Jackson 编解码序列化
     * @return
     */
    @Bean
    public MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter() {
        final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = converter.getObjectMapper();
        // 为mapper注册一个带有SerializerModifier的Factory，此modifier主要做的事情为：当序列化类型为array，list、set时，当值为空时，序列化成[]
        mapper.setSerializerFactory(mapper.getSerializerFactory().withSerializerModifier(new MyBeanSerializerModifier()));  

        //converter.setSupportedMediaTypes(ImmutableList.of(MediaType.TEXT_HTML, MediaType.APPLICATION_JSON));
        return converter;
    }
    
    /**
     * 定时任务 -多线程池
     * @return
     */
    @Bean(name="processExecutor")
    public TaskExecutor workExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setThreadNamePrefix("SysTask-");
        threadPoolTaskExecutor.setCorePoolSize(10);
        threadPoolTaskExecutor.setMaxPoolSize(20);
        threadPoolTaskExecutor.setQueueCapacity(600);
        threadPoolTaskExecutor.afterPropertiesSet();
        return threadPoolTaskExecutor;
    }
    
//    @Bean  
//    public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {  
//        ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);  
//        dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);  
//        return registration;  
//    } 
    
//    /**  
//     * 文件上传配置  
//     * @return  
//     */  
//    @Bean  
//    public MultipartConfigElement multipartConfigElement() {  
//        MultipartConfigFactory factory = new MultipartConfigFactory();  
//        //单个文件最大  
//        factory.setMaxFileSize("10240KB"); //KB,MB  
//        /// 设置总上传数据总大小  
//        factory.setMaxRequestSize("102400KB");  
//        return factory.createMultipartConfig();  
//    }    
}
